home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / Other Langs / Tickle-4.0 (tcl) / tcl / expecTerm / pty_sgi.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-11-17  |  2.3 KB  |  116 lines  |  [TEXT/MPS ]

  1. /* pty_sgi4.0.1.c - routines to allocate ptys - silicon graphics version
  2.  
  3. Written by: Don Libes, NIST, 6/22/91
  4.  
  5. Design and implementation of this program was paid for by U.S. tax
  6. dollars.  Therefore it is public domain.  However, the author and NIST
  7. would appreciate credit if this program or parts of it are used.
  8.  
  9. */
  10.  
  11. #include <unistd.h>
  12. #include <sys/types.h>
  13. #include <sys/stat.h>
  14. #include <sys/ioctl.h>
  15. #include <sys/file.h>
  16. #include <sys/fcntl.h>
  17. #include <sys/termio.h>
  18. #include "translate.h"
  19. #include <sys/sysmacros.h>
  20. #include <sys/stat.h>
  21. #include <stdio.h>
  22.  
  23. void debuglog();
  24.  
  25. #ifndef TRUE
  26. #define TRUE 1
  27. #define FALSE 0
  28. #endif
  29.  
  30. char    *line;
  31.  
  32. static void
  33. pty_stty(s,name)
  34. char *s;        /* args to stty */
  35. char *name;        /* name of pty */
  36. {
  37. #define MAX_ARGLIST 10240
  38.     char buf[MAX_ARGLIST];    /* overkill is easier */
  39.  
  40.     sprintf(buf,"stty %s < %s > %s",s,name,name);
  41.     system(buf);
  42. }
  43.  
  44. struct    termio exp_tty_original;
  45.  
  46. #define GET_TTYTYPE    0
  47. #define SET_TTYTYPE    1
  48. static void
  49. ttytype(request,fd,s)
  50. int request;
  51. int fd;
  52. char *s;    /* stty args, used only if request == SET_TTYTYPE */
  53. {
  54.     static int is_a_tty;
  55.  
  56.     if (request == GET_TTYTYPE) {
  57.         if (-1 == ioctl(fd, TCGETA, (char *)&exp_tty_original)) {
  58.             is_a_tty = FALSE;
  59.         } else is_a_tty = TRUE;
  60.     } else {    /* type == SET_TTYTYPE */
  61.         if (is_a_tty) {
  62.             (void) ioctl(fd, TCSETA, (char *)&exp_tty_original);
  63.         } else {
  64.             /* if running in the background, we have no access */
  65.             /* to a a tty to copy parameters from, so use ones */
  66.             /* supplied by original Makefile */
  67.             debuglog("getptyslave: (default) stty %s\n",DFLT_STTY);
  68.             pty_stty(DFLT_STTY,line);
  69.         }
  70.         if (s) {
  71.             /* give user a chance to override any terminal parms */
  72.             debuglog("getptyslave: (user-requested) stty %s\n",s);
  73.             pty_stty(s,line);
  74.         }
  75.     }
  76. }
  77.  
  78. void
  79. init_pty()
  80. {
  81.     ttytype(GET_TTYTYPE,0,(char *)0);
  82. }
  83.  
  84.  
  85. /* returns fd of master end of pseudotty */
  86. int
  87. getptymaster()
  88. {
  89.    int fd;
  90.    
  91.    line = _getpty(&fd, O_RDWR, 0600, 0);
  92.    if (line == NULL)   return (-1);
  93.  
  94.    return(fd);
  95. }
  96.  
  97.  
  98.  
  99. int
  100. getptyslave(stty_args)
  101. char *stty_args;
  102. {
  103.     int slave;
  104.  
  105.     if (0 > (slave = open(line, O_RDWR))) return(-1);
  106.  
  107.     /* sanity check - if slave not 0, skip rest of this and return */
  108.     /* to what will later be detected as an error in caller */
  109.     if (0 != slave) return(slave);
  110.  
  111.     fcntl(0,F_DUPFD,1);    /* duplicate 0 onto 1 to prepare for stty */
  112.     ttytype(SET_TTYTYPE,slave,stty_args);
  113.     return(slave);
  114. }
  115.  
  116.